# Figure B.3: Individual Vote Choice Difficulty


# 1. Load Packages ----

library(tidyverse)
library(ggpubr)
library(haven)

# 2. Read in Data ----

load(file = "df_voxit_individual_allrefs.RData")

# 3. Data Preparation ----
# Difficulty by legal form
diff_prop_type <- voxit_complete_salience %>%
  group_by(typex) %>%
  mutate(typex = as.numeric(haven::as_factor(typex))) %>%
  summarise(se = sd(difficulty, na.rm = T)/sqrt(n()),
            difficulty = mean(difficulty, na.rm = T)) %>%
  mutate(ci_95 = se*1.96)

# Relationship between Difficulty and Turnout
abstimmungen <- voxit_complete_salience %>%
  group_by(datum_merge) %>%
  summarise(turnout = mean(turnout, na.rm = T)*100,
            salience = mean(salience, na.rm = T),
            turnout_admin = mean(turnout_admin, na.rm = T),
            difficulty = mean(difficulty),
            nobs=sum(!is.na(turnout)))


# 4. Create Graph ----
# Distribution
dist_diff <- ggplot(data = voxit_complete_salience) +
  geom_histogram(mapping = aes(x = difficulty, y = (..count..)/sum(..count..)),
                 fill = "grey", color = "black", binwidth = 0.5) +
  scale_x_continuous(name = "Decision difficulty",
                     breaks = c(0, 1),
                     labels = c("No", "Yes")) +
  scale_y_continuous(name = "Relative frequency",
                     breaks = seq(0,0.6,0.1),
                     labels = scales::percent_format(accuracy = 5L),
                     limits = c(0,0.6)) +
  theme_bw(base_size = 9) +
  ggtitle(label = "Distribution of Decision Difficulty") +
  theme(plot.title = element_text(size = 12))


# graphs
diff_prop <- ggplot(data = diff_prop_type, 
                    mapping = aes(x = factor(typex, levels = c(3,2,1,4)),
                                  y = difficulty)) +
  geom_point() +
  geom_errorbar(mapping = aes(ymin = difficulty-ci_95, ymax = difficulty+ci_95),
                width = 0.2) +
  #  geom_text(mapping = aes(label = round(salience, digits = 1)), vjust = -0.4) +
  scale_y_continuous(name = "Average decision difficulty",
                     breaks = seq(0.38,0.5,0.02),
                     labels = scales::percent_format(accuracy = 2L),
                     limits = c(0.38,0.5)) +
  scale_x_discrete(name = "",
                   breaks = c(3,2,1,4),
                   labels = c("Compulsory \n referendum", "Optional \n referendum",
                              "Popular \n initiative", "Counter \n proposition")) +
  theme_bw(base_size = 9) +
  ggtitle(label = "Decision Difficulty by Legal Form") +
  theme(plot.title = element_text(size = 12))

ind_diff <- ggplot(data = abstimmungen, 
                   mapping = aes(x = difficulty, y = turnout)) +
  geom_point() +
  theme_bw(base_size = 9) +
  ylab("Average turnout") +
  scale_y_continuous(limits = c(20,100),
                     breaks = seq(20,100,10),
                     labels = c("20%", "30%", "40%", 
                                "50%", "60%", "70%", 
                                "80%", "90%", "100%")) +
  scale_x_continuous(limits = c(0.2,0.7),
                     breaks = seq(0.2,0.7,0.1),
                     labels = seq(0.2,0.7,0.1)) +
  xlab("Average decision difficulty") +
  ggtitle("Self-Reported Turnout and Decision Difficulty")  +
  theme(plot.title = element_text(size = 12))


admin_diff <- ggplot(data = abstimmungen, 
                     mapping = aes(x = difficulty, y = turnout_admin)) +
  geom_point() +
  theme_bw(base_size = 9) +
  ylab("Average turnout") +
  scale_y_continuous(limits = c(20,100),
                     breaks = seq(20,100,10),
                     labels = c("20%", "30%", "40%", 
                                "50%", "60%", "70%", 
                                "80%", "90%", "100%")) +
  scale_x_continuous(limits = c(0.2,0.7),
                     breaks = seq(0.2,0.7,0.1),
                     labels = seq(0.2,0.7,0.1)) +
  xlab("Average decision difficulty") +
  ggtitle("Administrative Turnout and Decision Difficulty")  +
  theme(plot.title = element_text(size = 12))

# 5. Create Graph ----
# Final Graph
ggarrange(dist_diff, diff_prop, ind_diff, admin_diff, ncol = 2, nrow = 2)
ggsave(filename = "FigureB3.pdf", width = 9.35, height = 5.58)

